<?php
declare (strict_types=1);

namespace app\callback\controller;

use app\admin\model\base\User;
use app\admin\model\base\UserOfficial;
use app\BaseController;
use app\Request;
use platform\MeiPaPlatform;
use think\response\Json;

/**
 * 美葩平台
 */
class MeiPa extends BaseController
{

    /**
     * 接收回调信息
     * @param Request $request
     * @return Json
     */
    public function meiPaAfterNotify(Request $request): Json
    {
        $post_data = file_get_contents("php://input");
        if (!$post_data) {
            $post_data = $request->all();
        }
        $post_data = is_array($post_data) ? $post_data : json_decode($post_data, true);
        //验证参数签名
        $MeiPa = new MeiPaPlatform();
        $signature = $MeiPa->sign($post_data['time'], $post_data['openid'], $post_data['carry_data']);
        if ($post_data['sing'] != $signature) {
            return json('error', 403);
        }
        //获取用户ID
        $user_id = User::where(['uuid' => $post_data['openid']])->value('id');
        if (!$user_id) {
            return json('error', 403);
        }
        //记录数据库
        $data = ['subscribe' => 1,];
        $where = [
            'user_id' => $user_id,
            'appid' => $post_data['wechat_appid'],
            'third_id' => 6,
        ];
        UserOfficial::update($data, $where);
        return json('success');
    }


}
